﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Windows.Forms
{
    /// <summary>
    /// 表示具有选项卡的控件基类
    /// </summary>
    public class TabControlBase : ControlBase
    {
        /// <inheritdoc cref="TabControlBase"/>
        protected TabControlBase()
        {

        }

        /// <summary>
        /// 更改主面板控件
        /// </summary>
        /// <typeparam name="TControl">待变更到的控件类型</typeparam>
        /// <param name="container">要放到哪个容器</param>
        /// <param name="args">控制的构造函数所需要的参数</param>
        /// <returns></returns>
        protected virtual TControl ChangeControl<TControl>(Control container, params object[] args)
            where TControl : UserControl, new()
        {
            return ChangeControl(container, typeof(TControl), args) as TControl;
        }

        /// <summary>
        /// 更改主面板控件
        /// </summary>
        /// <param name="controlType">待变更到的控件类型</param>
        /// <param name="container">要放到哪个容器</param>
        /// <param name="args">控制的构造函数所需要的参数</param>
        /// <returns></returns>
        protected virtual Control ChangeControl(Control container, Type controlType, params object[] args)
        {
            Control c = default;
            bool isExists = false;
            foreach (Control control in container.Controls)
            {
                if (control.GetType() == controlType)
                {
                    control.Visible = true;
                    c = control as Control;
                    isExists = true;
                }
                else
                {
                    control.Visible = false;
                }
            }
            if (!isExists)
            {
                var control = (Control)Activator.CreateInstance(controlType, args);
                control.Dock = DockStyle.Fill;
                container.Controls.Add(control);
                c = control;
            }

            return c;
        }

        /// <summary>
        /// 更改主面板控件
        /// </summary>
        /// <param name="instance">待变更到的控件</param>
        /// <param name="container">要放到哪个容器</param>
        /// <returns></returns>
        protected virtual Control ChangeControl(Control container, Control instance)
        {
            Control c = default;
            bool isExists = false;
            foreach (Control control in container.Controls)
            {
                if (control.GetType() == instance.GetType())
                {
                    control.Visible = true;
                    c = control as Control;
                    isExists = true;
                }
                else
                {
                    control.Visible = false;
                }
            }
            if (!isExists)
            {
                instance.Dock = DockStyle.Fill;
                container.Controls.Add(instance);
                c = instance;
            }

            return c;
        }
    }
}
